package com.walala.logic.worker;

import com.walala.logic.common.Constant;
import com.walala.logic.pojo.ChatMsgBean;
import com.walala.logic.protocol.IMProto;
import com.walala.logic.protocol.ProtocolPackage;
import com.walala.logic.service.SendDataService;
import com.walala.logic.utils.ProtoMessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author KayLi
 */
public class SendMsgWorker implements Runnable{

    private Logger logger = LoggerFactory.getLogger(SendMsgWorker.class);

    private ProtocolPackage pack;
    private ChatMsgBean msg;

    public SendMsgWorker(ProtocolPackage pack, ChatMsgBean msg){
        this.pack = pack;
        this.msg = msg;
    }

    @Override
    public void run() {

        int sessionType = msg.getSessionType();
        if(Constant.Session_Type.single.value == sessionType){
            sendSingle();
        }

    }

    private void sendSingle() {

        try {
            ProtocolPackage pp = pack.deepCopy();
            IMProto.ChatMsg chatMsg = ProtoMessageUtil.chatMsgBeanToPb(msg);
            pp.setProtoBody(chatMsg.toByteArray());
            SendDataService.send(pack.getReceiverId(),pp);
        } catch (Exception e) {
            logger.error("send msg worker error,senderId:{},receiverId:{},clientId:{},traceId:{}",pack.getSenderId(),pack.getReceiverId(),
                    pack.getClientType(),pack.getTraceId(),e);
        }

    }
}
